const EventEmitter = require('../../common/EventEmitter.js');
const requester = require('../../common/requester.js');
const config = require('../../common/config.js');
const util = require('../../common/util.js');
const $$onLoad = require('../../appLoaded/onLoad');

const app = getApp();
Page({
    pageIndex: 1,
    data: {
        actions: ['goPicList'],
        toView: '',
        hideViewMore: true,
        hasLoaded: false,
        inputStatus: false,
        isAuthed: true,
        sendButtonEnable: false
    },

    onLoad: function (param) {
        $$onLoad()
            .then(() => {
                let isAuthed = app.globalData.isAuthed;

                this.setData({
                    isAuthed,
                    id: param.id
                });

                if (isAuthed) {
                    this.init();
                }
            });
    },

    init: function () {
        this.getImgData();
        this.getRecommendResources(false);

        this.setData({
            scrollTop: 'top'
        })
    },
    // 单个图片点击
    imageTap: function (e) {
        let image = e.currentTarget.dataset.image;
        let curImageUrl = image.picUrl || image.smallPicUrl;
        let urls = [curImageUrl];
        wx.previewImage({
            current: curImageUrl,
            urls: urls
        });
    },

    //每次进入页面更新评论数和点赞数
    getImgData: function () {
        let id = this.data.id;
        requester.request({
            url: requester.service.resourceDetail,
            data: {
                resourceId: id,
            }
        }).then(res => {
            this.setData({
                image: res.data.userResource,
                showPicView: true
            });
        });
    },

    onPullDownRefresh: function () {
        this.setData({
            isRefreshing: true
        });
    },

    onPullDownStop: function () {
        wx.stopPullDownRefresh();
        this.setData({
            isRefreshing: false
        });
    },

    loadMore: function (e) {
        this.getRecommendResources(true);
    },
    /**
     * 获取数据。
     * @param isLoadMore boolean 是否是加载更多。
     */
    getRecommendResources: function (isLoadMore) {
        let me = this;

        if (isLoadMore) {
            wx.showLoading({
                title:'加载中...'
            });
            this.pageIndex++;
        } else {
            this.pageIndex = 1;
        }

        this.getResourceList()
            .then(resourceLi => {
                    util.addModeToList(resourceLi);
                    if (isLoadMore) {
                        let tmpLi = me.data.recommendList || [];
                        resourceLi = tmpLi.concat(resourceLi)
                    }
                    me.setData({
                        recommendList: resourceLi
                    }, () => {
                        this.onPullDownStop();
                        wx.hideLoading({
                            icon:'loading'
                        });
                    })
                }
            );
    },

    /**
     * 获取资源列表
     */
    getResourceList: function () {
        let pageIndex = this.pageIndex;
        return requester.request({
            url: requester.service.resourceList,
            data: {
                pageIndex: pageIndex
            }
        }).then(res => {
            return res.data.groupList;
        });
    },

    imgLoaded: function () {
        setTimeout(() => {
            this.setData({
                hasLoaded: true
            })
        }, 240)
    },

    //详情页查看更多图片，在当前页面上刷新
    cellTap: function (e) {
        var curImage = e.currentTarget.dataset.image;
        wx.redirectTo({
            url: `/pages/pictureDetail/pictureDetail?id=${curImage.id}`
        })
    },

    onShareAppMessage: function (res) {
        return {
            title: '图说馆，送你一张美图',
            path: '/pages/pictureDetail/pictureDetail?id=' + this.data.id,
            success: function (res) {
                // 转发成功
            },
            fail: function (res) {
                // 转发失败
            }
        }
    }
});